package com.boot.security.server.user.dao;

import com.boot.security.server.user.model.SysUsers;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserDao {

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into sys_user(username, password, mwPassword, nickname, ul, email, status, createTime, updateTime, branch, user_id) values(#{username}, #{password}, #{mwPassword}, #{nickname}, #{ul}, #{email}, #{status}, now(), now(), #{branch}, #{userId})")
    int save (SysUsers user);

    @Select("select * from sys_user t where t.id = #{id}")
    SysUsers getById (Long id);

    @Select("select * from sys_user t where t.username = #{username}")
    SysUsers getUser (String username);
    @Select("select * from sys_user t where t.oa = #{username}")
    SysUsers getUserByOa (String username);

    @Update("update sys_user t set t.password = #{password}, t.mwPassword = #{newPassword} where t.id = #{id}")
    int changePassword (@Param("id") Long id, @Param("password") String password, @Param("newPassword") String newPassword);

    Integer count (@Param("params") Map<String, Object> params);

    List<SysUsers> list (@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    @Delete("delete from sys_role_user where userId = #{userId}")
    int deleteUserRole (Long userId);

    int saveUserRoles (@Param("userId") Long userId, @Param("roleIds") List<Long> roleIds);

    int update (SysUsers user);

    /**
     * 获取审核管理员下所有的单位
     * @return
     */
    @Select("SELECT branch FROM sys_user WHERE id IN (SELECT userId FROM sys_role_user WHERE roleId = 5)")
    List<String> getBranch ();

    /**
     * 获取审核管理员下所有的单位同时排除自己
     * @param id
     * @return
     */
    @Select("SELECT branch FROM sys_user WHERE id IN (SELECT userId FROM sys_role_user WHERE roleId = 5) AND id != #{userId}")
    List<String> getBranchById (Long id);

    /**
     * 根据部门获取审核管理员下的用户
     * @param branch
     * @return
     */
    @Select("select * from sys_user where find_in_set(#{branch}, branch) and id IN (SELECT userId FROM sys_role_user WHERE roleId = 5)")
    List<SysUsers> findUserByLikeBranchAndReview (String branch);

    /**
     * 根据部门获取督办管理员下的用户
     * @param branch
     * @return
     */
    @Select("select * from sys_user where find_in_set(#{branch}, branch) and id IN (SELECT userId FROM sys_role_user WHERE roleId = 4)")
    List<SysUsers> findUserByLikeBranchAndSupervise (String branch);

    /**
     * 根据部门获取审核管理员下的用户同时排除自己
     * @param branch
     * @param id
     * @return
     */
    @Select("select * from sys_user where find_in_set(#{branch}, branch) and id IN (SELECT userId FROM sys_role_user WHERE roleId = 5) AND id != #{id}")
    List<SysUsers> findUserByLikeBranchAndReviewAndId (String branch, Long id);

    /**
     * 根据部门获取督办管理员下的用户同时排除自己
     * @param branch
     * @param id
     * @return
     */
    @Select("select * from sys_user where find_in_set(#{branch}, branch) and id IN (SELECT userId FROM sys_role_user WHERE roleId = 4) AND id != #{id}")
    List<SysUsers> findUserByLikeBranchAndSuperviseAndId (String branch, Long id);
    @Select("select * from sys_user where user_id = #{userId}")
    List<SysUsers> getByPid(Integer userId);
    @Select("select nickname from sys_user where username = #{oversee}")
    String getUserName(String oversee);
}
